<template>
  <el-dialog
    :title="storeSettingsTitle"
    :visible.sync="storeSettingsDialogVisible"
    width="30%"
    :close-on-click-modal="false"
    :before-close="storeSettingsDialogClose"
    @close="closeDialog"
  >
    <div>
      <el-form
        ref="storeSettingsForm"
        :model="storeSettingsForm"
        label-width="110px"
        :rules="storeSettingsRules"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="策略名称" prop="policyName">
              <el-input v-model="storeSettingsForm.policyName" placeholder="请输入策略名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item  label="文件组成格式" prop="fileFsmt">
              <selectData
                :selectFormat="selectFormat"
                :arNoPolicy="storeSettingsForm"
                pholder="请选择文件组成格式"
              />
            </el-form-item>
          </el-col>
        </el-row>

          <el-row>
          <el-col :span="12" class="save-strategy" >
            <span class="mark">*</span>
            <el-form-item label="存储策略" >

              <selectData
                :selectFormat="selectFormat"
                :arNoPolicy="storageStrategy"
                pholder="请选择存储策略"
              />
            </el-form-item>
          </el-col>
<!--          <el-col :span="12">
            <el-form-item label="是否启用">
              <el-radio-group v-model="storeSettingsForm.isOpen">
                <el-radio label="0">是</el-radio>
                <el-radio label="1">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>-->
        </el-row>

       <el-row>
         <el-col :span="24">
           <el-form-item label="描述">
             <el-input type="textarea" v-model="storeSettingsForm.remark"></el-input>
           </el-form-item>
         </el-col>
       </el-row>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="storeSettingsConfirm">确 定</el-button>
      <el-button @click="storeSettingsDialogClose">取 消</el-button>
    </span>

  </el-dialog>
</template>

<script>
import selectData from '@/components/selectData'
import { addSaveSettings, updateSaveSettings } from '@/api/system/sort'
export default {
  name: 'storeSettingsDialog',
  props:['storeSettingsForm','storeSettingsDialogVisible','storeSettingsTitle','queryParams','getSave','selectFormat'],
  components:{
    selectData
  },
  data() {
    return {
      storageStrategy:{
        fileFsmt:''
      },
      storeSettingsRules:{
        policyName: [
          { required: true, message: '请输入策略名称', trigger: 'blur' },
        ],
        fileFsmt: [
          { required: true, message: '请选择文件组成格式', trigger: 'blur' },
        ],
      },
    };
  },
  methods:{
    closeDialog(){
      for (let key in this.storeSettingsForm) {
        this.storeSettingsForm[key] = ''
      }
      this.storageStrategy.fileFsmt = ''
    },
    storeSettingsDialogClose(){
      this.$emit('closeSSettings')
    },
    //新增、修改存储设置
    storeSettingsConfirm(){
      this.$refs.storeSettingsForm.validate((valid) => {
        if (!valid) {
          return
        }
        if (/[\u4E00-\u9FA5]/g.test(this.storageStrategy.fileFsmt)) {
          return this.$message.error('编码不能输入汉字!')
        }
        if (/[\u4E00-\u9FA5]/g.test(this.storeSettingsForm.fileFsmt)) {
          return this.$message.error('编码不能输入汉字!')
        }
        if(!this.storageStrategy.fileFsmt){
          return this.$message.error('请选择存储策略')
        }
        this.$confirm('确认提交吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          const params = this.storeSettingsForm
          params.policyPath = this.storageStrategy.fileFsmt
          params.sortNo = this.queryParams.sortPrantNo
          if(this.storeSettingsTitle === '新增存储设置'){
            this.add(params)
          }else {
            this.update(params)
          }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消提交'
          });
        });
      });

    },
    // 新增存储设置
    async add(params){
      const res = await addSaveSettings(params)
      if(res.code === 200){
        this.$message.success(res.msg)
        this.storeSettingsDialogClose()
        this.getSave()
      }
    },
    // 修改存储设置
    async update(params){
      const res = await updateSaveSettings(params)
      if(res.code === 200){
        this.$message.success(res.msg)
        this.storeSettingsDialogClose()
        this.getSave()
      }
    }
  },
  mounted() {

  },
  watch:{
    storeSettingsForm:{
      immediate:true,
      handler(val){
        this.storageStrategy.fileFsmt = val.policyPath
      }
    }
  }
}
</script>

<style lang="scss" scoped>

.save-strategy{
  position: relative;
  .mark{
    position: absolute;
    top: 11px;
    left: 32px;
    color: red;
  }
}
</style>

